// Main player class; represents the main character of the game as well as the
// real-life person controlling him.
Player = util.extend(
	
  // Base
  jsgig.entity.Entity,
	
	function Player (num) {
    jsgig.entity.Entity.call(this);
    this.num = num ? parseInt(num) : 0;
    this.direction = "right";
    this.oldDirection = "right";
    this.state = "stand";
    this.oldState = "stand";
    var img = this.setImage(new jsgig.image.Animation({
      src: "assets/images/charles.png",
      size: [16, 16],
    }));
    img.importState("standright", {
      cellOffset: [0, 4],
      length: 1,
      flip: [false, false]
    });
    img.deriveState("standright", "standleft", {flip: [true, false]});
    img.importState("walkright", {
      cellOffset: [0, 5],
      length: 8,
      speed: 0.4,
      flip: [false, false]
    });
    img.deriveState("walkright", "walkleft", {flip: [true, false]});
    img.loadState(this.state + this.direction);
	},
	
	function /* override */ update() {
    jsgig.entity.Entity.prototype.update.call(this);
    
    this.state = "stand";
    var vi = jsgig.vi[this.num];
    if(!vi.allOf("up", "down")) {
      if (vi.up) {
        this.setY(this.getY() - 2);
        this.state = "walk";
      }
      else if (vi.down) {
        this.setY(this.getY() + 2);
        this.state = "walk";
      }
    }
    if (!vi.allOf("left", "right")) {
      if (vi.left) {
        this.setX(this.getX() - 2);
        this.direction = "left";
        this.state = "walk";
      }
      else if (vi.right) {
        this.setX(this.getX() + 2);
        this.direction = "right";
        this.state = "walk";
      }
    }
    
    if (this.state != this.oldState || this.direction !== this.oldDirection) {
      var img = this.getImage();
      img.loadState(this.state + this.direction);
      img.setFrame(0);
    }
    this.oldState = this.state;
    this.oldDirection = this.direction;
  }
);
